// =======================================================================
// 
//       Filename:  page.cpp
// 
//    Description:  
// 
//        Version:  1.0
//        Created:  27.03.2010 19:37:30
//       Revision:  none
//       Compiler:  g++
// 
//         Author:  Dominik 'domachine' Burgdörfer (-), dominik.burgdoerfer@googlemail.com
//        Company:  -
// 
// =======================================================================

#include "page.hpp"
#include "../navigation/node.hpp"
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <iostream>
#include <yaml.h>
#define foreach BOOST_FOREACH

using namespace std;
using namespace boost::filesystem;
using boost::shared_ptr;

namespace webplodder {
    namespace page {
        Page::Page(const path& sourcePath,
                const path& outputPath)
            : map<string,string>(), m_sourcePath(sourcePath),
            m_outputPath(outputPath), m_pos(0), m_inMenu(true),
            m_contentStack(sourcePath)
        {
        }

        void Page::writeContent(ostream& target) {
            ifstream in(m_sourcePath.string().c_str());
            boost::regex endRegex("^\\-\\-\\-[ \t]*$");

            while(!in.eof()) {
                string line;

                getline(in, line);
                if(boost::regex_match(line, endRegex))
                    break;
            }

            char ch;
            while(in.get(ch))
                target << ch;
        }

        ifstream* Page::contentStream() {
            ifstream* in = 
                    new ifstream(m_sourcePath.string().c_str());
            boost::regex endRegex("^\\-\\-\\-[ \t]*$");

            while(!in->eof()) {
                string line;

                getline(*in, line);
                if(boost::regex_match(line, endRegex))
                    break;
            }

            return in;
        }

        void Page::installNavigation(const vector<navigation::Node>& navi)
        {
            path parentPath = m_outputPath.parent_path();

            foreach(navigation::Node node, navi) {
                path wishPath(".");
                path currentPath = node.href();
                path::iterator pPathIter = parentPath.begin();
                path::iterator cPathIter = currentPath.begin();

                while(*pPathIter == *cPathIter) {
                    ++pPathIter;
                    ++cPathIter;

                    if(pPathIter == parentPath.end())
                        break;
                }

                while(pPathIter != parentPath.end()) {
                    wishPath /= "..";
                    ++pPathIter;
                }

                while(cPathIter != currentPath.end()) {
                    wishPath /= *cPathIter;
                    ++cPathIter;
                }

                m_navigation.push_back(navigation::Node(node.title(), 
                            wishPath.string(),
                            node.pos()));
            }
        }

        path Page::sourcePath() const
        {
            return m_sourcePath;
        }

        path Page::outputPath() const
        {
            return m_outputPath;
        }

        vector<navigation::Node>& Page::navigation() {
            return m_navigation;
        }

        int Page::pos() const
        {
            return m_pos;
        }

        bool Page::inMenu() const
        {
            return m_inMenu;
        }

        ElementStack& Page::content() {
            return m_contentStack;
        }
    }
}
